package com.yurun.orgpermissionmodule.service.Impl;

import com.github.pagehelper.PageHelper;
import com.yurun.commonapi.api.CommonResult;
import com.yurun.commonapi.pojo.UmsRole;
import com.yurun.orgpermissionmodule.mapper.OrgPermissionMapper;
import com.yurun.orgpermissionmodule.service.OrgPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class OrgPermissionServiceImpl implements OrgPermissionService {

    @Autowired
    private OrgPermissionMapper permissionModuleMapper;

    @Override
    public CommonResult addRole(UmsRole role) {
        role.setCreateTime(new Date());
        role.setUpdateTime(new Date());
        if (permissionModuleMapper.addRole(role) == 1) { // 增加角色成功
            int roleId = role.getId();//permissionModuleMapper.getRoleIdByOrgId(role.getOrganizationId()); // 获取roleId
            String str[] = role.getRoleIds().split(",");
            int array[] = new int[str.length];
            for (int i = 0; i < array.length; i++) {
                array[i]=Integer.parseInt(str[i]);
                System.out.println(array[i]);
            }

            for (int i = 0; i < array.length; i++) {
                if (array[i] < 1000) { // 小于1000是菜单id
                    permissionModuleMapper.addMenuByRoleId(roleId, array[i]);
                } else { // 大于1000是权限id
                    permissionModuleMapper.addPermissionByRoleId(roleId, array[i]);
                }
            }
            return CommonResult.success("新增成功！");
        } else {
            return CommonResult.failed("新增失败！");
        }
    }

    @Override
    public CommonResult allRoleInfo(BigInteger organizationId, String keyword, int pageNum, int pageSize) {
        System.out.println(keyword);
        if (keyword.equals("")) { // 查询机构所有角色
            PageHelper.startPage(pageNum, pageSize);
            return CommonResult.success(permissionModuleMapper.getRoleListByOrgId(organizationId));
        } else { // 根据角色名和机构id查
            PageHelper.startPage(pageNum, pageSize);
            return CommonResult.success(permissionModuleMapper.getRoleListByRoleName(keyword, organizationId));
        }
    }

    @Override
    public CommonResult getOneRoleInfo(int id) {
        List<Integer> roleIds = new ArrayList<>();
        HashMap map = new HashMap();
        UmsRole role = permissionModuleMapper.getRoleByRoleId(id);
        roleIds.addAll(permissionModuleMapper.getMenuIdByRoleId(id));
        roleIds.addAll(permissionModuleMapper.getResourceIdByRoleId(id));
        map.put("name", role.getName());
        map.put("status", role.getStatus());
        map.put("description", role.getDescription());
        map.put("roleIds", roleIds);
        return CommonResult.success(map);
    }

    @Override
    public CommonResult updateRoleStatus(int id) {
        if (permissionModuleMapper.updateRoleStatus(id, 0) == 1) {
            return CommonResult.success("更新成功！");
        } else {
            return CommonResult.success("更新失败！");
        }
    }

    @Override
    public CommonResult editRole(int roleId, int[] roleIds, String name, String description, int status) {
        UmsRole role = new UmsRole();
        role.setUpdateTime(new Date());
        role.setName(name);
        role.setDescription(description);
        role.setStatus(status);
        role.setId(roleId);
        System.out.println("roleId"+roleId);
        if (permissionModuleMapper.editRole(role) == 1) {
            permissionModuleMapper.delMenu(roleId); // 删除全部菜单
            permissionModuleMapper.delResource(roleId); // 删除全部权限
            for (int id : roleIds) {
                if (id < 1000) { // 更新菜单
                    //permissionModuleMapper.editRoleMenu(roleId, id);
                    permissionModuleMapper.addMenuByRoleId(roleId, id);
                    System.out.println("菜单"+id);
                } else { // 更新权限
                    //permissionModuleMapper.editRoleResource(roleId, id);
                    permissionModuleMapper.addPermissionByRoleId(roleId, id);
                    System.out.println("权限"+id);
                }
            }
            return CommonResult.success("修改成功！");
        }
        return CommonResult.success("修改失败！");
    }

    @Override
    public CommonResult getRoleOption(BigInteger organizationId) {
        return CommonResult.success(permissionModuleMapper.getRoleOption(organizationId));
    }
}
